package cn.luo.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

//用户表
@Document("t_user")
public class User implements UserDetails {
    @Id
    private String id;   //主键

    @Indexed    //建立索引
    private String username;   //用户名
    private String password;   //密码
    private String sex;         //性别
    private String nickname;   //昵称
    private String mailbox;    //邮箱
    private String role;        //角色权限
    private String img;         //头像图片

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> lst = new ArrayList<>();
        //this.role = "G,P"
        if (this.role == null) return null;
        String aa[] = this.role.split(",");  //进行字符串分割
        for (String s : aa) {
            SimpleGrantedAuthority sga = new SimpleGrantedAuthority(s.trim());
            System.out.println(s);
            lst.add(sga);
        }
        return lst;
    }

    //密码是什么？
    @Override
    public String getPassword() {
        return this.password;
    }

    //账号是什么？
    @Override
    public String getUsername() {
        return this.username;
    }


    //账号是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //账号是否锁住
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //密码是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //账号是否激活
    @Override
    public boolean isEnabled() {
        return true;
    }



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getMailbox() {
        return mailbox;
    }

    public void setMailbox(String mailbox) {
        this.mailbox = mailbox;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", nickname='" + nickname + '\'' +
                ", mailbox='" + mailbox + '\'' +
                ", role='" + role + '\'' +
                ", img='" + img + '\'' +
                '}';
    }
}
